import _ from 'lodash'
import Mock from 'mockjs'
import { success, error } from '@/utils/apiResult'

export const getUserList = params => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      let userList = getUserListFromStorage()
      userList = userList.filter(item => item.name.indexOf(params.name) >= 0)
      const total = userList.length
      const start = (params.current - 1) * params.size
      const end = start + params.size
      const userPage = {
        current: params.current,
        size: params.size,
        total: total,
        records: userList.slice(start, end)
      }
      resolve(success(userPage))
    }, 500)
  })
}

export const getUser = id => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const user = _.find(getUserListFromStorage(), item => item.id === id)
      resolve(success(user))
    }, 500)
  })
}

export const addUser = data => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      let userList = getUserListFromStorage()
      if (isExistsPhone(userList, null, data.phone)) {
        reject(error('手机号重复，请更换。'))
        return
      }

      userList.push({
        id: new Date().getTime(),
        ...data
      })
      setUserListToStorage(userList)
      resolve()
    }, 500)
  })
}

export const batchAddUser = () => {
  return new Promise((resolve, reject) => {
    let userList = getUserListFromStorage()
    let Random = Mock.Random
    let idInitValue = new Date().getTime()
    for (let i = 0; i < 50; i++) {
      userList.push({
        id: --idInitValue,
        phone: Random.integer(130, 139) + Random.string('number', 8),
        name: Random.cname(),
        gender: Random.pick('男', '女', '保密'),
        birthday: Random.date('yyyy-MM-dd'),
        address: Random.county(true)
      })
    }
    setUserListToStorage(userList)
    resolve()
  })
}

export const updateUser = data => {
  return new Promise((resolve, reject) => {
    let userList = getUserListFromStorage()
    if (isExistsPhone(userList, data.id, data.phone)) {
      reject(error('手机号重复，请更换。'))
      return
    }
    const index = _.findIndex(userList, item => item.id === data.id)
    if (index >= 0) {
      userList[index] = data
      setUserListToStorage(userList)
    }
    resolve()
  })
}

export const removeUser = id => {
  return new Promise((resolve, reject) => {
    let userList = getUserListFromStorage()
    _.remove(userList, item => item.id === id)
    setUserListToStorage(userList)
    resolve()
  })
}

function isExistsPhone(userList, id, phone) {
  if (phone) {
    return (
      _.findIndex(userList, item => {
        if (id) {
          return item.id !== id && item.phone === phone
        }
        console.log(item.phone === phone)
        return item.phone === phone
      }) >= 0
    )
  }
  return false
}

function getUserListFromStorage() {
  let userList = JSON.parse(sessionStorage.getItem('userList'))
  if (!userList) {
    userList = []
  }
  return userList
}

function setUserListToStorage(userList) {
  if (userList) {
    sessionStorage.setItem('userList', JSON.stringify(userList))
  }
}
